#ifndef __BASIC_LIBRARY2_TIMER_H__
#define __BASIC_LIBRARY2_TIMER_H__

#include "library/libobject.h"
#include "library/libexception.h"
#include <apr.h>
#include <apr_time.h>
#include <map>

namespace basic
{
    namespace library2
    {
        class ITimerItem : public basic::library2::CObject
        {
        public:
	        virtual apr_uint64_t GetHashCode() const = 0;
	        virtual std::string ToString() const = 0;
            virtual apr_status_t TimeOutCallBack(const apr_time_t &ddwCurrTime, const void *psArg) = 0;
        };
        typedef apr_status_t (*handle_timeout)(const apr_time_t &ddwCurrTime, const void *psArg);
        typedef apr_status_t (ITimerItem::*handle_timer_callback)(const apr_time_t &ddwCurrTime, const void *psArg);

        class ITimerQueue: public basic::library2::CObject
        {
        public:
	        virtual apr_uint64_t GetHashCode() const = 0;
	        virtual std::string ToString() const = 0;

            virtual bool HasTimeOutEvent(apr_time_t ddwCurrTime) = 0;
            virtual apr_status_t Excute(apr_time_t ddwCurrTime) = 0;

            virtual apr_status_t ScheduleTimer(handle_timeout pTimeOutProcess,	const void *psArg, apr_uint32_t dwDelay = 0, apr_uint32_t dwInterval = 0) = 0;
	        virtual apr_status_t CancelTimer (handle_timeout pTimeOutProcess) = 0;

            virtual apr_status_t ScheduleTimer(ITimerItem *poItem, const void *psArg, apr_uint32_t dwDelay = 0, apr_uint32_t dwInterval = 0) = 0;
	        virtual apr_status_t CancelTimer (ITimerItem *poItem) = 0;

            virtual ~ITimerQueue(void) {};
        private:
            //ITimerQueue(void) {};
        };

        //class CSimpleTimerItem  : public ITimerItem
        //{
        //public:
	       // virtual apr_uint64_t GetHashCode() const 
        //    {
        //        return (apr_uint64_t)this;
        //    };
	       // virtual std::string ToString() const 
        //    {
        //        return "CSimpleTimerItem";
        //    };
        //    apr_status_t TimeOutCallBack(const apr_time_t &ddwCurrTime, const void *psArg);
        //    apr_uint32_t dwInterval;
        //    handle_timeout pTimeOutProcess;
        //    const void *psArg;
        //};
        class CSimpleTimerQueue : public ITimerQueue
        {
        public:
	        virtual apr_uint64_t GetHashCode() const 
            {
                return (apr_uint64_t)this;
            };
	        virtual std::string ToString() const 
            {
                return "CSimpleTimerQueue";
            };

            CSimpleTimerQueue(void);
            virtual ~CSimpleTimerQueue(void);
            virtual bool HasTimeOutEvent(apr_time_t ddwCurrTime) ;
            virtual apr_status_t Excute(apr_time_t ddwCurrTime) ;
	        virtual apr_status_t ScheduleTimer(handle_timeout pTimeOutProcess,	const void *psArg, apr_uint32_t dwDelay = 0, apr_uint32_t dwInterval = 0) ;
	        virtual apr_status_t CancelTimer (handle_timeout pTimeOutProcess);

            virtual apr_status_t ScheduleTimer(ITimerItem *poItem, const void *psArg, apr_uint32_t dwDelay = 0, apr_uint32_t dwInterval = 0) ;
	        virtual apr_status_t CancelTimer (ITimerItem *poItem);

        private:
            struct TIMER_EVENT
            {
                apr_uint32_t dwInterval;
                handle_timeout pTimeOutProcess;
                ITimerItem *poItem;
                const void *psArg;
            };
            std::multimap<apr_time_t, TIMER_EVENT> m_astTimerEventList;
        };

    }
}

#endif
